Network Programming in Java: Internet protocols (IP, UDP, TCP) by Alan MOUHLI
Author:Alan MOUHLI
Language: eng
Format: mobi
Publisher: Alan MOUHLI
Published: 2016-04-05T21:00:00+00:00
import java.util.*; import java.io.*; import java.net.*;
public class TimeSender implements AutoCloseable {
private final DatagramSocket socket; private final DatagramPacket packet;
public TimeSender(int port) throws SocketException {
this.socket = new DatagramSocket();
this.packet = new DatagramPacket(new byte[0], 0); // Packet with empty array
this.packet.setPort(port);
}
public void sendTimePacket(InetAddress a) throws IOException
{
// We reuse the same datagram packet but modify its content and addressee
byte[] timeData = new
Date().toString().getBytes();
packet.setData(timeData);
packet.setAddress(a);
// this.packet.setPort is useless since the port does not change
socket.send(packet); // May throw an IOException }
public static InetAddress getSuccessor(InetAddress a) {
// Exercise: write the implementation of getSuccessor(InetAddress) returning the successor of an IP address.
// Example: the successor of 10.1.2.3 is 10.1.2.4 (for last byte 0 and 255 are forbidden)
}
public void close() throws IOException
{
socket.close();
}
public static void main(String[] args) throws IOException {
if (args.length < 3) throw new
IllegalArgumentException("Not enough arguments"); InetAddress firstA =
InetAddress.getByName(args[0]); // Start address InetAddress lastA =
InetAddress.getByName(args[1]); // Stop address int port = Integer.parseInt(args[2]); try (TimeSender ts = new TimeSender(port)) {
for (InetAddress currentA = firstA; !
currentA.equals(lastA); currentA = getSuccessor(currentA)) ts.sendTimePacket(currentA); }
}
}
Some tips for the exchange of packets
Recycle DatagramPacket already created (by changing the hosted data)
Do not create unnecessary DatagramSocket, release them when no longer needed
Always check that the receipt size is sufficient
classic mistake: send k byte packet and receive on the same package without changing table or size; it is then limited to k bytes for reception
Do not forget to use the size of the data received: the end of the table is not exploitable
Check the IP address / transmitter port of a received packet (even if it makes only a small safety)
Mechanism of pseudo-connection
Possibility of pseudo-connect two UDP sockets with void
DatagramSocket.connect (InetAddress addr, int port): automatic filtering of packets sent and received
Checking the connection withisConnected boolean ()
Disconnection with void disconnect ()
Some tests with Netcat
Netcat: simple application to communicate with UDP (and TCP)
We run the server on port UDP N listening on all interfaces (wildcard address): nc -l -u N
It runs a client connecting to the port N of the machine:
n -u N MachineName
The client sends each line of standard input in a UDP datagram: the server receives and displays it on its standard output
server datagram sending to the customer also possible
Java example: receive datagrams on a port with a deadline
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Whiskies (Collins Gem) by dominic roskrow(45523)
Spell It Out by David Crystal(36232)
Cecilia; Or, Memoirs of an Heiress — Volume 1 by Fanny Burney(32716)
Cecilia; Or, Memoirs of an Heiress — Volume 2 by Fanny Burney(32083)
Cecilia; Or, Memoirs of an Heiress — Volume 3 by Fanny Burney(32063)
Beautiful Disaster by McGuire Jamie(25452)
Trainspotting by Irvine Welsh(21965)
Chic & Unique Celebration Cakes by Zoe Clark(20147)
The Secret History by Donna Tartt(19465)
How High Can a Kangaroo Hop? by Jackie French(18843)
Twilight of the Idols With the Antichrist and Ecce Homo by Friedrich Nietzsche(18775)
All the Missing Girls by Megan Miranda(16684)
Cat's cradle by Kurt Vonnegut(15549)
Ready Player One by Cline Ernest(15139)
For the Love of Europe by Rick Steves(14892)
Fifty Shades Freed by E L James(13346)
4 3 2 1: A Novel by Paul Auster(12609)
Crooked Kingdom: Book 2 (Six of Crows) by Bardugo Leigh(12447)
Grundlagen Kreatives Schreiben (German Edition) by Helfferich Pia(10521)